package com.hu.pk.patroller.mp;

import com.hu.pk.patroller.dto.OrderDto;
import com.rabbitmq.client.Channel;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;

@Component
@Slf4j
public class OrderConsumer {

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitHandler
    @RabbitListener(queues = "order-dead-letter-queue")
    public void handleOrder(@Payload OrderDto orderDto, Channel channel, Message message) {

/*        HashMap<String, Object> map = new HashMap<>();
        map.put("id",orderDto.getId());
        map.put("roadId",orderDto.getRoadId());
        map.put("carLicensePlate",orderDto.getCarLicensePlate());
        map.put("parkingTime",orderDto.getParkingTime());
        map.put("userTel",orderDto.getUserTel());
        map.put("berthId",orderDto.getBerthId());
        map.put("driveInTime",orderDto.getDriveInTime());
        map.put("driveOutTime",orderDto.getDriveOutTime());
        map.put("unclaimed:road"+orderDto.getRoadId()+":berth:"+orderDto.getBerthId(),orderDto);*/
        log.info("死信队列处理消息：{}",orderDto);
        redisTemplate.opsForHash().put("unclaimed","unclaimed:road"+orderDto.getRoadId()+":berth:"+orderDto.getBerthId(),orderDto);


//        redisTemplate.opsForHash().putAll("unclaimed:road"+orderDto.getRoadId()+":berth:"+orderDto.getBerthId(),map);

/*        try {
            //手动应答
            log.info("channel.basicAck start");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.info("channel.basicAck end");
        } catch (IOException e) {
            e.printStackTrace();
        }*/

    }
}